package com.example.lanchat.ui.conn;

import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.bluetooth.BluetoothDevice;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.lanchat.MainActivity;
import com.example.lanchat.R;
import com.example.lanchat.databinding.ConnFragmentBinding;
import com.example.lanchat.databinding.FragmentGalleryBinding;
import com.example.lanchat.ui.gallery.GalleryViewModel;

import java.util.ArrayList;
import java.util.List;

public class ConnFragment extends Fragment {

    private ConnViewModel mViewModel;
    private ConnFragmentBinding binding;
    MainActivity activity;
    RecyclerView recyclerView;
    MyAdapter myAdapter;
    RadioGroup radioGroup;
    public List<BluetoothDevice> myBTDeviceList = new ArrayList<>();

    public static ConnFragment newInstance() {
        return new ConnFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        mViewModel =
                new ViewModelProvider(this).get(ConnViewModel.class);
        binding = ConnFragmentBinding.inflate(inflater, container, false);
        activity = (MainActivity) getActivity();
        Button scan = binding.onScan;
        Button inform = binding.inform;
        recyclerView = binding.DeviceRecyclerView;
        myAdapter = new MyAdapter();
        recyclerView.setAdapter(myAdapter);
        radioGroup = binding.myRadioGroup;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(activity);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setVisibility(View.VISIBLE);
        binding.inform.setVisibility(View.VISIBLE);
        binding.startAdvertising.setVisibility(View.INVISIBLE);
        //扫描按钮监听
        scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("tag", "已扫描");
                activity.myBLEScan(true);
            }
        });
        //通知按钮监听
        inform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.i("tag", "已开启");
                activity.myNotifySet(true);
            }
        });
        View root = binding.getRoot();
        //观察myBTDeviceLiveData并更新RecyclerView控件
        activity.myBTDeviceLiveData.observe(getViewLifecycleOwner(), new Observer<List>() {
            @Override
            public void onChanged(List list) {
                myBTDeviceList = list;
                myAdapter.notifyDataSetChanged();
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                Log.i("mytag", "i是：" + i);
                Log.i("mytag", "master是：" + R.id.master + "slave是：" + R.id.slave);

                switch (i) {
                    case R.id.master:
                        recyclerView.setVisibility(View.VISIBLE);
                        binding.inform.setVisibility(View.VISIBLE);
                        binding.startAdvertising.setVisibility(View.INVISIBLE);
                        activity.isMaster = true;
                        break;
                    case R.id.slave:
                        recyclerView.setVisibility(View.INVISIBLE);
                        binding.inform.setVisibility(View.INVISIBLE);
                        binding.startAdvertising.setVisibility(View.VISIBLE);
                        activity.isMaster = false;
                        break;

                    default:
                        break;
                }
            }
        });
        binding.startAdvertising.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                activity.startAdvertising();
            }
        });
        return root;
    }

    //设置RecyclerView的适配器
    class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(), R.layout.conn_item, null);
            MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.name.setText(myBTDeviceList.get(position).getName());
            holder.mac.setText(myBTDeviceList.get(position).getAddress());
            holder.linearLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    activity.connectWithIndex(holder.getAdapterPosition());
                    Toast.makeText(activity, "点击了第" + holder.getAdapterPosition(), Toast.LENGTH_LONG).show();
                    Log.i("tag", "点击了第" + holder.getAdapterPosition());
                }
            });
        }

        @Override
        public int getItemCount() {
            Log.i("tag", "size" + myBTDeviceList.size());
            return myBTDeviceList.size();
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(ConnViewModel.class);
        // TODO: Use the ViewModel
    }

}